<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<link rel="stylesheet" href="../../layui/css/formSelects-v4.css" media="all">
<link rel="stylesheet" href="../../statics/lib/layui/css/layui.css" media="all">
<body>
<hr>
<!--搜索框-->
<form class="layui-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">物品名称</label>
            <div class="layui-input-inline" style="width: 200px;">
                <input placeholder="请输入物品名称关键字" style="" id="goodsNameId" type="text" name="price_min"
                       autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">种类名</label>
            <div class="layui-input-inline" style="width: 300px;">
                <select xm-select="kindIds" name="" id="kindIds">
                </select>
            </div>
            <div class="layui-input-inline" style="width: 100px;">
                <button type="button" class="layui-btn layui-btn-normal" onclick="searchData();"><i
                        class="layui-icon layui-icon-search"></i> 查询
                </button>
            </div>
        </div>

    </div>
</form>
<!--表格上方工具栏-->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <shiro:hasPermission name="goods:add">
            <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="addGoods()"><i
                    class="layui-icon layui-icon-add-circle"></i>添加
            </button>
        </shiro:hasPermission>
    </div>
</script>


<table class="layui-hide" id="test" lay-filter="test"></table>
</body>

<script>

    $.post("/kinds/getAllKinds", function (result) {
        if (result.code === 0) {
            debugger;
            for (var i = 0; i < result.data.length; i++) {
                var opt = $("<option></option>").appendTo("#kindIds");
                opt.text(result.data[i].kindsName).val(result.data[i].id);
            }
            layui.formSelects.render();
        }
    });
    var table;
    layui.use(["table", "form", "formSelects"], function () {
        table = layui.table;
        table.render({
            elem: '#test'
            , url: '/goods/getAllGoods'
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , title: '物品数据表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', sort: true, fixed: 'left', width: 70}
                , {field: 'goodsName', title: '物品名称', width: 100}
                , {field: 'goodsDesc', title: '物品描述', width: 100}
                , {field: 'kindsName', title: '物品种类', width: 100}
                , {field: 'goodsNo', title: '物品编号', width: 100}
                , {field: 'goodsNum', title: '物品数量', width: 100}
                , {
                    field: 'goodsState', title: '在库状态', width: 100, templet: function (d) {
                        var str = d.goodsState.trim() == "在库" ? "checked" : "";
                        var a = '<input  type="checkbox" disabled  ' + str + '  />';
                        return a;
                    }
                }
                , {field: 'goodsPosition', title: '物品位置', width: 150}
                , {
                    field: 'createTime', title: '创建时间',
                    templet: function (d) {
                        return dateToStr(d.createTime);
                    }, width: 180
                }
                , {
                    field: 'modifyTime', title: '修改时间',
                    templet: function (d) {
                        return dateToStr(d.modifyTime);
                    }, width: 180
                }
                , {field: 'createPerson', title: '创建人', width: 80}
                , {field: 'modifyPerson', title: '修改人', width: 80}
                , {
                    title: '工具', templet: function (d) {
                        return borrowHandle(d);
                    }
                }
            ]]
            , page: true
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            debugger;
            if (obj.event === 'del') {
                layer.confirm('确定删除？', function (index) {
                    $.ajax({
                        url: "/goods/deleteGoodsInfo",
                        data: {id: data.id, goodsState: data.goodsState},
                        success: function (result) {
                            if (0 == result.code) {
                                layer.msg(result.msg, {icon: 1, time: 1000});
                                layer.close(index);
                                //刷新首页
                                searchData();
                            } else {
                                layer.msg(result.msg, {icon: 5, time: 2000});
                            }
                        }
                    });
                });
            } else if (obj.event === 'modify') {
                var lay = layer.open({
                    type: 2,
                    title: "物品信息修改",
                    shade: 0.4,
                    area: ['800px', '600px'],
                    maxmin: true,
                    shadeClose: true,
                    content: '/doModifyGoodsInfo',
                    fix: false, //不固定
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);//少了这个是不能从父页面向子页面传值的
                        body.contents().find('#id').val(data.id);
                        //获取子页面的元素，进行数据渲染
                        body.contents().find("#goodsName").val(data.goodsName);
                        body.contents().find('#goodsDesc').val(data.goodsDesc);
                        body.contents().find('#kindsName').val(data.kindsName);
                        body.contents().find('#goodsNo').val(data.goodsNo);
                        body.contents().find('#goodsPosition').val(data.goodsPosition);
                    },
                    end: function () {
                        searchData();
                    }

                });
            } else if (obj.event === "borrow") {
                if (true == data.needReback) {
                    $.ajax({
                        method: "post",
                        url: "/goods/goodsHandleState",
                        data: {goodsId: data.id, selectBorrowNum: data.goodsNum},
                        success: function (result) {
                            if (0 === result.code) {
                                layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                    searchData();
                                });

                            } else {
                                layer.msg(result.msg, {icon: 5, time: 1000}, function () {
                                    searchData();
                                });
                            }
                        }
                    })
                } else {
                    var lay = layer.open({
                        type: 2,
                        title: "选择数量",
                        shade: 0.4,
                        area: ['320', '180px'],
                        maxmin: true,
                        shadeClose: true,
                        content: '/doSelectBorrowNum',
                        fix: false, //不固定
                        success: function (layero, index) {
                            var body = layer.getChildFrame('body', index);//少了这个是不能从父页面向子页面传值的
                            body.contents().find('#id').val(data.id);
                        },
                        end: function () {
                            searchData();
                        }
                    })
                }
            }
        });
    });

    function dateToStr(date) {
        if (null == date || "" === date) {
            return "";
        }
        var time = new Date(date);
        var y = time.getFullYear();
        var M = time.getMonth() + 1;
        M = M < 10 ? ("0" + M) : M;
        var d = time.getDate();
        d = d < 10 ? ("0" + d) : d;
        var h = time.getHours();
        h = h < 10 ? ("0" + h) : h;
        var m = time.getMinutes();
        m = m < 10 ? ("0" + m) : m;
        var s = time.getSeconds();
        s = s < 10 ? ("0" + s) : s;
        var str = y + "-" + M + "-" + d + " " + h + ":" + m + ":" + s;
        return str;
    }

    function searchData() {
        var str = layui.formSelects.value('kindIds', 'varStr');
        var kindsIds = "";
        for (var i = 0; i < str.length; i++) {
            kindsIds += str[i].value;
            kindsIds += ",";
        }
        kindsIds = kindsIds.substring(0, kindsIds.length - 1);
        var page = $(".layui-laypage-em").next().html();
        layui.table.reload("test", {
            page: {
                curr: page
            },
            where: {
                "goodsName": $("#goodsNameId").val(),
                "kindsIds": kindsIds
            }
        });
    }

    function addGoods() {
        var lay = layer.open({
            type: 2,
            title: "添加物品信息",
            shade: 0.4,
            area: ['800px', '600px'],
            maxmin: true,
            shadeClose: true,
            content: '/doAddGoodsInfo',
            fix: false,
            end: function () {
                searchData();
            }
        });
    }

    function borrowHandle(d) {

        var buttonEnable = d.buttonEnable == true ? "borrow" : "";
        var background = d.buttonEnable == true ? "deepskyblue" : "gray";
        var editEnable = (d.handleState).trim() == "可借阅" ? "modify" : "";
        var editBackground = (d.handleState).trim() == "可借阅" ? "orange" : "gray";
        var tool = "";
        <shiro:hasPermission name="goods:modify">
            tool += '<a class="layui-btn layui-btn-xs" lay-event="' + editEnable + '" style="background:' + editBackground + ' "> <i class="layui-icon">&#xe63c;</i>编辑</a>';
        </shiro:hasPermission>
        <shiro:hasPermission name="goods:delete">
            tool += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"> <i class="layui-icon">&#xe640;</i>删除</a>';
        </shiro:hasPermission>
        <shiro:hasPermission name="goods:borrow">
            tool += '<a class="layui-btn layui-btn-danger layui-btn-xs" style="background: ' + background + '" lay-event="' + buttonEnable + '" id="goodsHandleStateId"> <i class="layui-icon"></i>' + d.goodsButtonWord + '</a>';
        </shiro:hasPermission>
        return tool;
    }
</script>
